
Ext.util.JSON4Java = new (function () {

	var getObjectType = function (o) {
		var type = typeof o;
		switch (type) {
		  case "undefined":
		  case "unknown":
		  case "function":
			return type;
		  case "object":
			if (o) {
				if (o.constructor == Array.prototype.constructor) {
					return "array";
				} else {
					if (o.constructor == Date.prototype.constructor) {
						return "date";
					} else {
						if (o._javaClass) {
							return o._javaClass;
						} else {
							return "object";
						}
					}
				}
			} else {
				return "null";
			}
		  case "number":
			return "number";
		  case "string":
			return "string";
		  case "boolean":
			return "boolean";
		  default:
			return "unknown";
		}
	};
	
	var formatInteger = function (n, length) {
		var s = "" + n;
		while (s.length < length) {
			s = "0" + s;
		}
		return s;
	};
	
	var toJSONObject = function (o) {
		var text = "";
		for (var p in o) {
			if (typeof (o[p]) != "function" && typeof (o[p]) != "undefined" && typeof (o[p]) != "unknown") {
				var name = new String(p);
				if (text.length > 0) {
					text += ",";
				}
				text += "\"" + name + "\":" + toJSONValue(o[p]);
			}
		}
		return "{" + text + "}";
	};
	
	var toJSONArray = function (o) {
		var text = "";
		for (var i = 0; i < o.length; i++) {
			if (text.length > 0) {
				text += ",";
			}
			text += toJSONValue(o[i]);
		}
		return "[" + text + "]";
	};
	
	var toJSONDate = function (d) {
		var string = "" + d.getFullYear() + "-" + formatInteger(d.getMonth() + 1, 2) + "-" + formatInteger(d.getDate(), 2) + " " + formatInteger(d.getHours(), 2) + ":" + formatInteger(d.getMinutes(), 2) + ":" + formatInteger(d.getSeconds(), 2) + "." + formatInteger(d.getMilliseconds(), 3);
		return "{\"string\":\"" + string + "\",\"time\":" + d.getTime() + ",\"_javaClass\":\"java.util.Date\"}";
	};
	
	var toJSONValue = function (o) {
		switch (getObjectType(o)) {
		  case "null":
		  case "boolean":
		  case "number":
		  case "string":
		  case "function":
		  case "undefined":
		  case "unknown":
			return Ext.encode(o);
		  case "array":
			return toJSONArray(o);
		  case "date":
			return toJSONDate(o);
		  default:
			return toJSONObject(o);
		}
	};
	
	var toJSObject = function (o) {
		var className = "";
		var names = new Array();
		var values = new Array();
		for (var p in o) {
			var name = new String(p);
			var value = o[p];
			if (name == "_javaClass") {
				className = o[p];
			} else {
				names[names.length] = name;
				values[values.length] = toJSValue(value);
			}
		}
		o = Ext.createObject(className);
		for (var i = 0; i < names.length; i++) {
			o[names[i]] = values[i];
		}
		return o;
	};
	
	var toJSArray = function (o) {
		var arr = new Array();
		for (var i = 0; i < o.length; i++) {
			arr[i] = toJSValue(o[i]);
		}
		return arr;
	};
	
	var toJSDate = function (o) {
		return new Date(o.time);
	};
	
	var toJSMap = function (o) {		
		return o;
	};
	
	var toJSValue = function (o) {
		switch (getObjectType(o)) {
		  case "null":
		  case "boolean":
		  case "number":
		  case "string":
		  case "function":
		  case "undefined":
		  case "unknown":
			return o;
		  case "array":
			return toJSArray(o);
		  case "java.util.Date":
			return toJSDate(o);
		  case "java.util.Map":
		    return toJSMap(o);
		  default:
			return toJSObject(o);
		}
	};
	
	this.encode = function (o) {
		return toJSONValue(o);
	};
	
	this.decode = function (s) {
		return toJSValue(Ext.decode(s));
	};
})();

Ext.createObject = function (className) {
	var obj = eval("(new " + className + "())");
	obj._javaClass = className;
	return obj;
};

var Plugin = {
	modules  : [],
	settings : [],
	programs : [],	
	messages : []
};

Plugin.Seperator = Ext.extend(Ext.app.Module, {
    init : function(){
        this.launcher = '-'
    }
});


Ext.namespace("org.jwos.platform.ajax");

org.jwos.platform.ajax.JSONRequest = function () {
	this.directReturn = false;
	this.method = null;
	this.parameter = null;
	this.service = null;
	this.type = null;
	this.sessionId = null;
	this._javaClass = "org.jwos.platform.ajax.JSONRequest";
};

org.jwos.platform.ajax.JSONResponse = function () {
	this.error = null;
	this.errorCode = null;
	this.result = null;
	this.success = false;
	this._javaClass = "org.jwos.platform.ajax.JSONResponse";
};

Ext.app.RemoteCall = function(config) {

	this.cfg = config || {};
	this.url = this.cfg.url;
	this.service = this.cfg.service;
	this.type = this.cfg.type;
	this.timeout = this.cfg.timeout || 60000;
	
	this.invoke = function (method, params, OnSuccess, scope, timeout) {
    
        Ext.lib.Ajax.setDefaultPostHeader(false);
        Ext.lib.Ajax.initHeader("Content-Type", "application/json; charset=utf-8");
        Ext.Ajax.timeout = timeout || this.timeout;
        
        var req = Ext.createObject("org.jwos.platform.ajax.JSONRequest");
	    req.service = this.service;
	    req.type = this.type;
	    req.method = method;
	    req.parameter = params;
	    if(MyDesktop && MyDesktop.currentSessionId) {
	    	req.sessionId = MyDesktop.currentSessionId;
	    }	    

        var transId = Ext.Ajax.request({
            url: this.url,
            method: 'POST',
            scriptTag: true,
            params: Ext.util.JSON4Java.encode(req),
            scope: scope,
            callback: function(options, success, response) {
                var res = Ext.util.JSON4Java.decode(response.responseText);
                var result = res.result;
                if (success) {                	
               		if(!res.success && res.errorCode && Plugin.messages[res.errorCode] && Plugin.messages[res.errorCode].length>0) {
               			res.error = Plugin.messages[res.errorCode];
               		}
                	if(OnSuccess) OnSuccess.call(scope, res.result, res.success, res.error, res.errorCode);
                } else {
                    var msg = (result && result.Message) ? result.Message : 'Some error happend. Please try again';
                    Ext.MessageBox.alert('Error('+response.status+'): '+response.statusText , msg);                            
                }
            }
        });
        
        Ext.lib.Ajax.setDefaultPostHeader(true);  
        Ext.Ajax.timeout = 60000;
        
        return transId;
    }
};

Ext.namespace("org.jwos.platform.domain");

org.jwos.platform.domain.User = function() {
   this.companyId = null;
   this.email = null;
   this.enable = null;
   this.groupId = null;
   this.id = null;
   this.locale = null;
   this.password = null;
   this.userId = null;
   this.userName = null;

   this._javaClass ="org.jwos.platform.domain.User";
};